import json
import requests
import config


def getTuple(prompt: str) -> str:
    # 定义默认参数
    DEFAULT_PARAMS = {
        "model": "gpt-3.5-turbo",
        "temperature": 0
    }

    # 从文件中读取文本内容
    with open(config.text_prompt_path, 'r', encoding='utf-8') as file:
        text = file.read()

    # 替换文本中的 "$prompt"
    new_prompt = text.replace("$prompt", prompt)
    # 构建参数对象
    params = {
        **DEFAULT_PARAMS,
        'messages': [{'role': 'user', 'content': new_prompt}],
        'stop': '\n'
    }
    # 构建请求头
    headers = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + config.gpt_key
    }
    # 构建请求体
    data = json.dumps(params)
    # 发送 POST 请求
    try:
        response = requests.post(config.gpt_url, headers=headers, data=data)
        response.raise_for_status()  # 抛出异常，如果请求不成功
        response_json = response.json()  # 解析 JSON 响应
        content = response_json['choices'][0]['message']['content']

        return content
        # 在这里处理响应
    except requests.exceptions.RequestException as e:
        # 捕获请求异常
        print("Something went wrong with the request:", e)
